package com.craftplay.middleware;

import com.craftplay.common.PageBean;
import com.craftplay.common.WxMpInfo;
import com.craftplay.dao.UnitMapper;
import com.craftplay.model.Unit;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UnitPageService {
    @Autowired
    private UnitMapper unitMapper;

    public PageBean<Unit> selectUnitList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Unit> list = unitMapper.findAll();
        return new PageBean<>(list);
    }

    public PageBean<Unit> selectUnitListByCondition(int flag, int pageNum, int pageSize, String address, int kind, String scope, int price, int dist, int ret, double latitude, double longitude) {
        PageHelper.startPage(pageNum, pageSize);
        List<Unit> list = null;
        //如果是前四种
        if (flag == WxMpInfo.FIRST_MENU) {
            //如果没有选择根据价格排序, 那么根据距离
            if (price == 0) {
                //如果从近到远
                if (dist == 0) {
                    //如果提供经纬度,按照位置
                    if (ret == 1) {
                        list = unitMapper.selectUnit(address, kind, scope, latitude, longitude);
                    } else {
                        //如果不提供经纬度,按照时间
                        list = unitMapper.selectUnitOrderByTime(address, kind, scope);
                    }

                } else {
                    //否则从远到近,暂时用不到
                    list = unitMapper.selectUnitDesc(address, kind, scope, latitude, longitude);
                }
            } else if (price == -1) {
                //选择根据价格排序, 从低到高
                list = unitMapper.selectUnitByPriceAsc(address, kind, scope);
            } else {
                //选择根据价格排序, 从高到低
                list = unitMapper.selectUnitByPriceDesc(address, kind, scope);
            }
            //否则是后面3种kind情况
        } else {

            if (price == 0) {
                //如果从近到远
                if (dist == 0) {
                    //如果提供经纬度,按照位置
                    if (ret == 1) {
                        list = unitMapper.selectUnitByCondition(address, kind, scope, latitude, longitude);
                    } else {
                        list = unitMapper.selectUnitOrderByTimeAndCondition(address, kind, scope);
                    }
                } else {
                    //否则从远到近,暂时用不到
                    list = unitMapper.selectUnitByConditionDesc(address, kind, scope, latitude, longitude);
                }
            } else if (price == -1) {
                //选择根据价格排序, 从低到高
                list = unitMapper.selectUnitByPriceAscT(address, kind, scope);
            } else {
                //选择根据价格排序, 从高到低
                list = unitMapper.selectUnitByPriceDescT(address, kind, scope);
            }


        }
        return new PageBean<>(list);
    }
}
